import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import progress from 'nprogress'
import 'nprogress/nprogress.css'

Vue.use(Router)

// reload token when page refresh
if (localStorage.getItem('token')) {
    store.commit('SET_TOKEN', localStorage.getItem('token'))
}

if (localStorage.getItem('role')) {
    store.commit('SET_ROLE', localStorage.getItem('role'))
}

let router = new Router({
    routes: [
        {
            path: '/',
            name: 'Homepage',
            meta: {
                guest: true
            },
            component: resolve => require(['@/page/homepage'], resolve)
        },
        {
            path: '/account/:id',
            meta: {
                guest: true
            },
            component: resolve => require(['@/page/account'], resolve)
        },
        {
            path: '/',
            component: resolve => require(['@/page/layout/basic'], resolve),
            children: [
                {
                    path: 'userinfo',
                    name: 'userinfo',
                    meta: {
                        guest: true
                    },
                    component: resolve => require(['@/page/account/userinfo'], resolve),
                    children: [
                        {
                            path: 'account',
                            name: 'account',
                            component: resolve => require(['@/page/account/userinfo/account'], resolve),
                            children: [
                                {
                                    path: 'account_pwd',
                                    name: 'account_pwd',
                                    component: resolve => require(['@/page/account/userinfo/account/account_pwd'], resolve)
                                },
                                {
                                    path: 'basic',
                                    name: 'basic',
                                    component: resolve => require(['@/page/account/userinfo/account/basic'], resolve)
                                }
                            ]
                        },
                        {
                            path: 'friends',
                            name: 'friends',
                            component: resolve => require(['@/page/account/userinfo/friends'], resolve),
                            children: [
                                {
                                    path: 'all',
                                    name: 'all',
                                    component: resolve => require(['@/page/account/userinfo/friends/all'], resolve)
                                }
                            ]
                        }
                    ]
                },
                {
                    path: 'schedule',
                    name: 'schedule',
                    component: resolve => require(['@/page/schedule'], resolve),
                    children: [
                    ]
                },
                {
                    path: 'artical',
                    name: 'artical',
                    component: resolve => require(['@/page/artical/index'], resolve),
                    children: [
                        {
                            path: 'square',
                            name: 'square',
                            component: resolve => require(['@/page/artical/square'], resolve)
                        },
                        {
                            path: 'my',
                            name: 'my',
                            component: resolve => require(['@/page/artical/my'], resolve)
                        },
                        {
                            path: 'favor',
                            name: 'favor',
                            component: resolve => require(['@/page/artical/favor'], resolve)
                        },
                        {
                            path: 'create',
                            name: 'create',
                            component: resolve => require(['@/page/artical/create'], resolve)
                        }
                    ]
                }
            ]
        }
    ],
    mode: 'history'
})

router.beforeEach((to, from, next) => {
    progress.start()
    if (to.meta.guest) {
        next()
        progress.done()
        return false
    }
    if (store.state.user.token) {
        next()
        progress.done()
        return false
    }
  // 路由跳转到失败，重定向进入登陆页面前，先存入路由，以备成功后跳转
  // next({path: '/account/login', query: {'redirect': to.path}})
    next({path: '/account/login', query: {'redirect': '/schedule'}})
    progress.done()
})

export default router
